# player
# moves to 4 points, when it reaches one, it goes to the next
(class playerClass (group

	# points
	(def dd_point p0)
	(def dd_point p1)
	(def dd_point p2)
	(def dd_point p3)

	# target's sprite
	(def dd_sprite targetSprite)

	# catmull rom curve
	(def float counter)
	(def dd_point cat0)
	(def dd_point cat1)
	(def dd_point cat2)
	(def dd_point cat3)

	# sprite and target
	(def dd_sprite sprite)
	(def dd_point target)

	(def int playerHeight)

	# maximum rotation (degrees)
	(def int maxRot)

	# control speed
	(def int speed)
	(def int positive)

	# init
	(function init (group) (group

		(= this.targetSprite (new dd_sprite "images/dd_logo.png"))
		(this.targetSprite.setWidth 50)
		(this.targetSprite.setHeight 50)
		(this.targetSprite.setAnchor 0.5)

		(= this.playerHeight 150)

		(= this.maxRot 20)
		(= this.speed 0)
		(= this.positive 1)

		# sprite
		(= this.sprite (new dd_sprite "images/player.png"))
		(this.sprite.setWidth 150)
		(this.sprite.setHeight this.playerHeight)
		#(this.sprite.setAnchor 0.5)

		# moving points
		(= this.p0 (new dd_point (/ (this.sprite.getWidth) 2) (/ (this.sprite.getHeight) 2)))
		(= this.p1 (new dd_point (- eng.width (* (this.sprite.getWidth) 1.5)) (/ (this.sprite.getHeight) 2)))
		(= this.p2 (new dd_point (- eng.width (* (this.sprite.getWidth) 1.5)) (- eng.height (* (this.sprite.getHeight) 1.5))))
		(= this.p3 (new dd_point (/ (this.sprite.getWidth) 2) (- eng.height (* (this.sprite.getWidth) 1.5))))
		(= this.target this.p0)

		(= this.cat3 (new dd_point 0 0))

		# init next target
		(this.nextTarget)
	))

	(function slowDown (group) (group
		(= this.positive 0)
	))

	# move to next target
	(function nextTarget (group) (group

		# reset counter
		(= this.counter 0)

		(def dd_point prevTarget this.target)

		# find the next target
		(if (== this.target this.p0) (= this.target this.p1)
		    (== this.target this.p1) (= this.target this.p2)
		    (== this.target this.p2) (= this.target this.p3)
		    (== this.target this.p3) (= this.target this.p0)
		)

		# set the position of the new target
		(this.targetSprite.setX (+ this.target.x (/ (this.sprite.getWidth ) 2)))
		(this.targetSprite.setY (+ this.target.y (/ (this.sprite.getHeight) 2)))

		# calculate catmull-rom points
		(= this.cat0 this.cat3)
		(= this.cat1 (new dd_point (this.sprite.getX) (this.sprite.getY)))
		(= this.cat2 this.target)
		(= this.cat3 (new dd_point (- (this.sprite.getX) this.cat3.x) (- (this.sprite.getY) this.cat3.y)))

		(= this.positive 1)

	)) # nextTarget

	# update
	(function update (group) (group

		# animate speed
		(if (&& (< this.speed 1) (== this.positive 1))
			(group
			(= this.speed (+ this.speed 0.05))
			(if (> this.speed 1) (= this.speed 1))
			)
		(&& (>= this.speed 0) (== this.positive 0))
			(group
			(= this.speed (- this.speed 0.05))
			(if (<= this.speed 0.2)
				(group
				(= this.speed 0.2)
				(this.nextTarget)
				)
			)
			)
		)

		# advance counter
		(= this.counter (+ this.counter (* 0.02 this.speed)))

		# find new position
		(def dd_point new_pos (new dd_point 0 0))
		(dd_math.catmullrom2d new_pos this.counter this.cat0 this.cat1 this.cat2 this.cat3)

		# rotate slightly to the left or right
		(def dd_point static_pos2 (new dd_point
			(- new_pos.x (this.sprite.getX))
			(- new_pos.y (this.sprite.getY))
		))

		# calculate new rotation and slowly rotate
		(def int new_rot (/ static_pos2.x 2))
		(if (> new_rot this.maxRot) (= new_rot this.maxRot)
		    (< new_rot (* this.maxRot -1)) (= new_rot (* this.maxRot -1))
		)
		(this.sprite.setRotationDegrees
			(+ (this.sprite.getRotationDegrees) (* (- new_rot (this.sprite.getRotationDegrees)) 0.1))
		)

		# scale slightly
		(if (> static_pos2.y 0)
			(this.sprite.setHeight
				(- this.playerHeight (* static_pos2.y 10))
			)

		# else
			(this.sprite.setHeight
				this.playerHeight
			)
		)

		# set new location
		(this.sprite.setX new_pos.x)
		(this.sprite.setY new_pos.y)

		# done with current animation, move to next target
		(if (>= this.counter 1) (group
			(this.nextTarget)
		))
	))

)) # player class
